package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisMessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.QiNiuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * Description: No Description
 * User: Eric
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealMobileController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 套餐列表
     * @return
     */
    @GetMapping("/getSetmeal")
    public Result findAll(){
        // 查询所有
        List<Setmeal> setmealList = setmealService.findAll();
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmealList);
    }

    /**
     * 套餐详情
     */
    @GetMapping("/findDetailById")
    public Result findDetailById(int id){
        Jedis jedis = jedisPool.getResource();
        String setMealIdStr = jedis.get(RedisMessageConstant.SETMEAL + id);

        if (null == setMealIdStr) {
            Setmeal setmeal = setmealService.findDetailById(id);
            if (null == setmeal) {
                jedis.setex(RedisMessageConstant.SETMEAL + id,60,null);
                return new Result(false,"查询套餐不存在");
            }
            setmeal.setImg(QiNiuUtils.DOMAIN+setmeal.getImg());
            setMealIdStr = JSON.toJSONString(setmeal);
            jedis.set(RedisMessageConstant.SETMEAL + id,setMealIdStr);
        }
        jedis.close();
        JSONObject jsonResult = JSON.parseObject(setMealIdStr);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,jsonResult);
    }

    /**
     * 查询套餐详情
     */
    @GetMapping("/findDetailById2")
    public Result findDetailById2(int id){
        // 调用服务查询详情
        Setmeal setmeal = setmealService.findDetailById2(id);
        // 设置图片的完整路径
        setmeal.setImg(QiNiuUtils.DOMAIN + setmeal.getImg());
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    /**
     * 查询套餐详情
     */
    @GetMapping("/findDetailById3")
    public Result findDetailById3(int id){
        // 调用服务查询详情
        Setmeal setmeal = setmealService.findDetailById3(id);
        // 设置图片的完整路径
        setmeal.setImg(QiNiuUtils.DOMAIN + setmeal.getImg());
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }


    /**
     * 通过id查询
     */
    @GetMapping("/findById")
    public Result findById(int id){
        // 套餐信息
        Setmeal setmeal = setmealService.findById(id);
        setmeal.setImg(QiNiuUtils.DOMAIN+setmeal.getImg());
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }
}
